package com.huangd.platform.common.utils;

import java.awt.geom.IllegalPathStateException;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtils {

	public static void unzip(File unzip, File target) throws Exception {

		if (target.isFile())
			throw new IllegalPathStateException("解压缩路径不允许是一个文件");

		if (!target.exists())
			target.mkdirs();

		ZipInputStream zis = new ZipInputStream(new FileInputStream(unzip));
		BufferedInputStream bis = new BufferedInputStream(zis);

		ZipEntry entry;
		while ((entry = zis.getNextEntry()) != null) {
			File out = new File(target, entry.getName());
			if (entry.isDirectory()) {
				out.mkdir();
				continue;
			}

			FileOutputStream fos = new FileOutputStream(out);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			int b;
			while ((b = bis.read()) != -1) {
				bos.write(b);
			}

			bos.close();
			fos.close();
		}
		bis.close();
		zis.close();
	}

	public static void main(String[] args) throws Exception {

		unzip(new File("E:\\version_upload\\server\\4.zip"), new File("E:\\version_upload\\server\\4"));
		System.out.println("finished");
	}
}
